<template>
  <div class="j-rate">
    <span
      v-for="item in max"
      class="j-rate__item"
      @mousemove="setCurrentValue(item, $event)"
      @mouseleave="resetCurrentValue"
      @click="selectValue(item)"
      :style="{ cursor: disabled ? 'auto' : 'pointer' }">
      <i
        :class="[classes[item - 1], { 'hover': hoverIndex === item }]"
        class="j-rate__icon"
        :style="getIconStyle(item)">
        <i
          v-if="showDecimalIcon(item)"
          :class="decimalIconClass"
          :style="decimalStyle"
          class="j-rate__decimal">
        </i>
      </i>
    </span>
    <span v-if="showText" class="j-rate__text" :style="{ color: textColor }">{{ text }}</span>
  </div>
</template>
<script>
  import { hasClass } from '~src/lib/class'
  export default {
    name: 'j-rate',
    data () {
      return {
        classMap: {},
        colorMap: {},
        classes: null,
        pointerAtLeftHalf: false,
        currentValue: this.value,
        hoverIndex: -1
      }
    },
    props: {
      value: {
        type: Number,
        default: 0
      },
      lowThreshold: {
        type: Number,
        default: 2
      },
      highThreshold: {
        type: Number,
        default: 4
      },
      max: {
        type: Number,
        default: 5
      },
      colors: {
        type: Array,
        default () {
          return ['#F7BA2A', '#F7BA2A', '#F7BA2A']
        }
      },
      voidColor: {
        type: String,
        default: '#C6D1DE'
      },
      disabledVoidColor: {
        type: String,
        default: '#EFF2F7'
      },
      iconClasses: {
        type: Array,
        default () {
          return ['j-icon-star-on', 'j-icon-star-on', 'j-icon-star-on']
        }
      },
      voidIconClass: {
        type: String,
        default: 'j-icon-star-off'
      },
      disabledVoidIconClass: {
        type: String,
        default: 'j-icon-star-on'
      },
      disabled: {
        type: Boolean,
        default: false
      },
      allowHalf: {
        type: Boolean,
        default: false
      },
      showText: {
        type: Boolean,
        default: false
      },
      textColor: {
        type: String,
        default: '1f2d3d'
      },
      texts: {
        type: Array,
        default () {
          return ['1分', '2分', '3分', '4分', '5分']
        }
      },
      textTemplate: {
        type: String,
        default: '{value}'
      }
    },

    computed: {
      text () {
        let result = ''
        if (this.disabled) {
          result = this.textTemplate.replace(/\{\s*value\s*\}/, this.value)
        } else {
          result = this.texts[Math.ceil(this.currentValue) - 1]
        }
        return result
      },

      decimalStyle () {
        let width = ''
        if (this.disabled) {
          width = (this.valueDecimal < 50 ? 0 : 50) + '%'
        }
        if (this.allowHalf) {
          width = '50%'
        }
        return {
          color: this.activeColor,
          width
        }
      },
      valueDecimal () {
        return this.value * 100 - Math.floor(this.value) * 100
      },
      decimalIconClass () {
        return this.getValueFromMap(this.value, this.classMap)
      },
      voidClass () {
        return this.disabled ? this.classMap.disabledVoidClass : this.classMap.voidClass
      },
      activeClass () {
        return this.getValueFromMap(this.currentValue, this.classMap)
      },
      activeColor () {
        return this.getValueFromMap(this.currentValue, this.colorMap)
      },
      classes () {
        let result = []
        let threshold = this.currentValue
        if (this.allowHalf && this.currentValue !== Math.floor(this.currentValue)) {
          threshold--
        }
        for (let i = 0; i < threshold; i++) {
          result.push(this.activeClass)
        }
        for (let a = 0; a < this.max; a++) {
          result.push(this.voidClass)
        }
        return result
      }
    },

    watch: {
      value (val) {
        this.$emit('change', val)
        this.currentValue = val
      }
    },

    methods: {
      getValueFromMap (value, map) {
        let result = ''
        if (value <= this.lowThreshold) {
          result = map.lowColor || map.lowClass
        } else if (value >= this.highThreshold) {
          result = map.highColor || map.highClass
        } else {
          result = map.mediumColor || map.mediumClass
        }
        return result
      },

      showDecimalIcon (item) {
        let showWhenDisabled = this.disabled && this.valueDecimal > 0 && item - 1 < this.value && item > this.value
        /* istanbul ignore next */
        let showWhenAllowHalf = this.allowHalf && this.pointerAtLeftHalf && ((item - 0.5).toFixed(1) === this.currentValue.toFixed(1))
        return showWhenDisabled || showWhenAllowHalf
      },

      getIconStyle (item) {
        const voidColor = this.disabled ? this.colorMap.disabledVoidColor : this.colorMap.voidColor
        return {
          color: item <= this.currentValue ? this.activeColor : voidColor
        }
      },

      selectValue (value) {
        if (this.disabled) {
          return
        }
        if (this.allowHalf && this.pointerAtLeftHalf) {
          this.$emit('input', this.currentValue)
        } else {
          this.$emit('input', value)
        }
      },

      setCurrentValue (value, event) {
        if (this.disabled) {
          return
        }
        /* istanbul ignore if */
        if (this.allowHalf) {
          let target = event.target
          if (hasClass(target, 'j-rate__item')) {
            target = target.querySelector('.j-rate__icon')
          }
          if (hasClass(target, 'j-rate__decimal')) {
            target = target.parentNode
          }
          this.pointerAtLeftHalf = event.offsetX * 2 <= target.clientWidth
          this.currentValue = this.pointerAtLeftHalf ? value - 0.5 : value
        } else {
          this.currentValue = value
        }
        this.hoverIndex = value
      },

      resetCurrentValue () {
        if (this.disabled) {
          return
        }
        if (this.allowHalf) {
          this.pointerAtLeftHalf = this.value !== Math.floor(this.value)
        }
        this.currentValue = this.value
        this.hoverIndex = -1
      }
    },

    created () {
      if (!this.value) {
        this.$emit('input', 0)
      }
      this.classMap = {
        lowClass: this.iconClasses[0],
        mediumClass: this.iconClasses[1],
        highClass: this.iconClasses[2],
        voidClass: this.voidIconClass,
        disabledVoidClass: this.disabledVoidIconClass
      }
      this.colorMap = {
        lowColor: this.colors[0],
        mediumColor: this.colors[1],
        highColor: this.colors[2],
        voidColor: this.voidColor,
        disabledVoidColor: this.disabledVoidColor
      }
    }
  }
</script>
<style lang="scss">
  .j-rate {
    height: 20px;
    line-height: 1
  }

  .j-rate__item {
    font-size: 0;
    vertical-align: middle
  }

  .j-rate__icon,.j-rate__item {
    display: inline-block;
    position: relative
  }

  .j-rate__icon {
    font-size: 18px;
    margin-right: 6px;
    color: #bfcbd9;
    transition: .3s
  }

  .j-rate__icon .path2 {
    position: absolute;
    left: 0;
    top: 0
  }

  .j-rate__icon.hover {
    transform: scale(1.15)
  }

  .j-rate__decimal {
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    overflow: hidden
  }

  .j-rate__text {
    font-size: 14px;
    vertical-align: middle
  }
</style>
